package ioperation;

import book.Book;
import book.Booklist;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 刘远望
 * Date: 2025-03-19
 * Time: 20:24
 */
public class AddOperation implements IOperation{
    @Override
    public void work(Booklist booklist) {
        System.out.println("新增图书");
        //判断是否已经满
        int currentsize = booklist.getUseSize();
        if(currentsize == booklist.getBooks().length){
            System.out.println("已经放满了");
            return;
        }
        //创建书
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要添加的书名");
        String name = scanner.nextLine();
        System.out.println("请输入作者");
        String author = scanner.nextLine();
        System.out.println("请输入价格");
        int price = scanner.nextInt();
        System.out.println("输入书的类型");
        String type = scanner.nextLine();

        Book newbook = new Book(name,author,price,type);
        //判断有没有这本书
        for (int i = 0; i < currentsize; i++) {
            Book book = booklist.getBooks()[i];
            if(book.getName().equals(name)){
                System.out.println("有这本书，不能插入");
                return;
            }
        }
        //插入图书
        booklist.setBook(currentsize, newbook);
        booklist.setUseSize(currentsize + 1);
        System.out.println("新图书加入成功");

    }
}
